package com.wei.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wei.client.VodClientFeign;
import com.wei.entity.Chapter;
import com.wei.entity.Course;
import com.wei.entity.CourseDescription;
import com.wei.entity.Video;
import com.wei.entity.vo.BaseCourseVo;
import com.wei.entity.vo.CourseInfo;
import com.wei.entity.vo.CoursePublishVo;
import com.wei.entity.vo.CourseQueryVo;
import com.wei.service.ChapterService;
import com.wei.service.CourseDescriptionService;
import com.wei.service.CourseService;
import com.wei.mapper.CourseMapper;
import com.wei.service.VideoService;
import com.wei.service_base.exception.XueYuanException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author HASEE
 * @description 针对表【edu_course(课程)】的数据库操作Service实现
 * @createDate 2022-11-23 09:57:54
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course>
        implements CourseService {
    @Autowired
    private CourseDescriptionService courseDescriptionService;

    @Autowired
    private VideoService videoService;

    @Autowired
    private ChapterService chapterService;

    @Autowired
    private VodClientFeign vodClientFeign;

    //添加课程信息 -- 包括基本信息和描述
    @Transactional
    @Override
    public String addCourseInfo(CourseInfo courseInfo) {

        Course course = new Course();
        BeanUtils.copyProperties(courseInfo, course);
        int insert = baseMapper.insert(course);

        if (insert <= 0) {
            throw new XueYuanException(20001, "插入数据失败");
        }

        //课程id
        String cid = course.getId();

        CourseDescription courseDescription = new CourseDescription();

        //一个课程对应一个课程描述信息
        courseDescription.setId(cid);
        courseDescription.setDescription(courseInfo.getDescription());

        courseDescriptionService.save(courseDescription);

        return cid;
    }

    @Override
    public CourseInfo queryCourseInfo(Long id) {

        Course course = baseMapper.selectById(id);

        CourseInfo courseInfo = new CourseInfo();

        BeanUtils.copyProperties(course, courseInfo);

        CourseDescription courseDescription = courseDescriptionService.getById(id);

        courseInfo.setDescription(courseDescription.getDescription());

        return courseInfo;
    }

    @Override
    public void updateCourseData(CourseInfo courseInfo) {

        Course course = new Course();
        BeanUtils.copyProperties(courseInfo, course);

        baseMapper.updateById(course);

        CourseDescription courseDescription = new CourseDescription();
        courseDescription.setId(courseInfo.getId());
        courseDescription.setDescription(courseInfo.getDescription());

        courseDescriptionService.updateById(courseDescription);
    }

    @Override
    public CoursePublishVo getPublishInfo(String courseId) {

        CoursePublishVo coursePublishInfo = baseMapper.getCoursePublishInfo(courseId);

        return coursePublishInfo;
    }

    @Override
    public void publishCourse(String courseId) {

        Course course = new Course();
        course.setId(courseId);
        course.setStatus("Normal");

        baseMapper.updateById(course);
    }

    /**
     * 删除课程，要删除全部小节、视频、章节和课程
     *
     * @param courseId
     */
    @Override
    public void deleteCourse(String courseId) {

        //TODO 删除视频
        //删除视频
        LambdaQueryWrapper<Video> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Video::getCourseId, courseId);

        List<Video> videoList = videoService.list(queryWrapper);


        List<String> stringList = videoList.stream().filter(video -> video.getVideoSourceId() != null)
                .map(Video::getVideoSourceId).collect(Collectors.toList());

        //批量删除视频
        vodClientFeign.deleteBatch(stringList);

        //删除小节
        LambdaQueryWrapper<Video> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(Video::getCourseId, courseId);

        videoService.remove(queryWrapper1);

        //删除章节
        LambdaQueryWrapper<Chapter> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.eq(Chapter::getCourseId, courseId);

        chapterService.remove(queryWrapper2);

        //删除描述信息
        courseDescriptionService.removeById(courseId);

        //删除课程
        baseMapper.deleteById(courseId);
    }


    @Override
    public Map<String, Object> queryCoursePage(Long current, Long limit, CourseQueryVo courseQueryVo) {

        Page<Course> page = new Page<>(current, limit);

        String oneSubject = courseQueryVo.getSubjectParentId();
        String twoSubject = courseQueryVo.getSubjectId();
        String buyCountSort = courseQueryVo.getBuyCountSort();
        String priceSort = courseQueryVo.getPriceSort();
        String gmtCreateSort = courseQueryVo.getGmtCreateSort();

        LambdaQueryWrapper<Course> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(StrUtil.isNotBlank(oneSubject), Course::getSubjectParentId, oneSubject)
                .eq(StrUtil.isNotBlank(twoSubject), Course::getSubjectId, twoSubject)
                .orderByDesc(StrUtil.isNotBlank(buyCountSort), Course::getBuyCount)
                .orderByDesc(StrUtil.isNotBlank(priceSort), Course::getPrice)
                .orderByDesc(StrUtil.isNotBlank(gmtCreateSort), Course::getGmtCreate);

        baseMapper.selectPage(page, queryWrapper);

        Map<String, Object> map = new HashMap<>();

        map.put("records", page.getRecords());
        map.put("current", page.getCurrent());
        map.put("size", page.getSize());
        map.put("total", page.getTotal());
        map.put("pages", page.getPages());
        map.put("hasNext", page.hasNext());
        map.put("hasPrevious", page.hasPrevious());

        return map;
    }

    @Override
    public BaseCourseVo getCourseInfoById(String courseId) {
        return baseMapper.queryBaseCourseInfo(courseId);
    }

}




